/**
 * 岗位授权申请列表
 */
<template>
	<vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="sqr">
							<template #label><vp-label zh="申请人" en="Applicant" /></template>
							<el-input v-model="searchForm.sqr" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
                        <el-form-item prop="gwmc">
                            <template #label><vp-label zh="岗位" en="Position" /></template>
                            <el-input v-model="searchForm.gwmc" readonly>
                                <template #append>
                                    <el-button @click="handleChoosePosition">
                                        <SvgIcon name="ele-Search" />
                                    </el-button>
                                </template>
                            </el-input>
                        </el-form-item>
                    </vp-search-col>
					<vp-search-col>
						<el-form-item prop="ztList">
							<template #label><vp-label zh="状态" en="Status"/></template>
							<VpStatusMultSelect v-model="searchForm.ztList" />
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event"/>
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #actions>
			<vp-btn-add v-auth="'qualityPostApply.edit'" @click="handleAdd"/>
		</template>
		<template #table>
			<vp-vxe-table
				id="postApplyTable"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column 
					field="zt" 
					show-overflow
					width="140"
				>
					<template #header><vp-label zh="状态" en="Status"/></template>
					<template #default="scope">
						<span>{{ getStatusDesc(scope.row.zt) }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					sortable
					field="bh" 
					show-overflow
					width="140"
				>
					<template #header><vp-label zh="编号" en="No."/></template>
					<template #default="scope">
						<span>{{ scope.row.bh }}</span>
					</template>
				</vxe-column>
				<vxe-column 
					field="sqrbh" 
					show-overflow
					width="140"
				>
					<template #header><vp-label zh="申请人编号" en="Applicant No."/></template>
				</vxe-column>
				<vxe-column 
					field="sqrxm" 
					show-overflow 
					min-width="150"
				>
					<template #header><vp-label zh="申请人姓名" en="Applicant Name" /></template>
				</vxe-column>
				<vxe-column 
					field="sqrq" 
					show-overflow 
					min-width="150"
				>
					<template #header><vp-label zh="申请日期" en="Application Date" /></template>
				</vxe-column>
				<vxe-column 
					field="sqgwmc" 
					show-overflow 
					min-width="120"
				>
					<template #header><vp-label zh="申请岗位" en="Authorization Position"/></template>
				</vxe-column>
				<vxe-column 
					sortable
					field="cjsj" 
					show-overflow 
					min-width="160"
				>
					<template #header><vp-label zh="制单时间" en="Crt. Time"/></template>
				</vxe-column>
				<vxe-column 
					field="operation" 
					title="操作 Operation"
					fixed="right" 
					width="180"
				>
					<template #header><vp-label-operations/></template>
					<template #default="scope">
						<el-space>
							<vp-btn-view text @click="handleView(scope.row.id)"/>
							<vp-btn-edit v-if="scope.row.zt === 10" text v-auth="'qualityPostApply.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt === 10" text v-auth="'qualityPostApply.edit'" @click="handleDelete(scope.row)"/>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<EditPostApply ref="editPostApplyRef" @formSubmited="handleFormSubmited"></EditPostApply>
		<VpPositionDialog ref="vpPositionDialogRef"></VpPositionDialog>
	</vp-main-page>
</template>
<script setup lang="ts" name="postApply">
import { onMounted, reactive, ref, defineAsyncComponent } from 'vue'
import request from '/@/utils/request';
import { useRouter } from 'vue-router';
import { DEFAULT_PAGE } from '/@/utils/page';
import VpMessageBox from '/@/components/vp/VpMessageBox';
import VpStatusMultSelect from "/@/components/vp/status/VpStatusMultSelect.vue";
import commonFunction from '/@/utils/commonFunction';

const EditPostApply = defineAsyncComponent(() => import('./component/EditQualityPostApply.vue'));
const VpPositionDialog = defineAsyncComponent(() => import('/@/components/vp/position/VpPositionDialog.vue'));

const searchForm = reactive({
	sqr: "",
	gwid: "",
	gwmc: "",
	ztList: [10, 88, 89, 98],
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

const router = useRouter();

const {getStatusDesc} = commonFunction();


const vpPositionDialogRef = ref()
const handleChoosePosition = () =>{
  vpPositionDialogRef.value.handleOpen({
    selected: searchForm.gwid,
    callback: (data: any) => {
        if(searchForm.gwid != data.id){
            searchForm.items = [];
        }
        searchForm.gwid = data.id;
        searchForm.gwmc = data.mc;
    }
  })
}

// 加载列表数据
const loadDataList = () => {
	loading.value = true;
	request.get('/quality/postApply/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
	searchForm.gwid = "";
    searchForm.gwmc = "";
	loadDataList();
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	loadDataList();
})

const editPostApplyRef = ref();
const tableRef = ref();

const handleAdd = ()=>{
	editPostApplyRef.value.handleOpen({
		type: 'add',
	});
}

const handleEdit = (row: any)=>{
	editPostApplyRef.value.handleOpen({
		type: 'edit',
		id: row.id
	});
}

const handleFormSubmited = ()=>{
	loadDataList();
}

const handleView = (id: string)=>{
	router.push({
		name: "viewQualityPostApply",
		params: {
			id: id,
		},
	})
}

// 删除
const handleDelete = (row: any)=>{
	VpMessageBox.confirmDelete().then(()=>{
		loading.value = true;
		request.delete(`/quality/postApply/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

</script>

<style scoped lang="scss">
</style>